package com.theobserver.theobserver;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

//import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;

import java.util.List;


public class NewsCommentActivity extends AppCompatActivity{
    private Handler mHandler = new Handler();
    //private ProgressBar mProgerss;
    private TextView mName;
    private TextView mComment;

    NewsCommentAdapter adapter = null;
    SwipeRefreshLayout sl = null;
    AVObject news = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newscomment);

        //mProgerss = (ProgressBar) findViewById(R.id.mProgess);

        //mName = (TextView) findViewById(R.id.TextView0);
        //mComment = (TextView) findViewById(R.id.TextView1);

        ListView lv = (ListView) findViewById(R.id.listview_news);
        adapter = new NewsCommentAdapter(NewsCommentActivity.this);
        lv.setAdapter(adapter);

        sl = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout_news);
        sl.setRefreshing(true);
        View add_more = (View) findViewById(R.id.relativelayout_news_addmore);
        add_more.setVisibility(View.GONE);



        //Intent newsid = getIntent();
        //String objectid = newsid.getStringExtra("objectId");
        String objectid = getIntent().getStringExtra("objectId");
        news = AVObject.createWithoutData("news", objectid);
        final MultiAutoCompleteTextView mCommentEdit = (MultiAutoCompleteTextView) findViewById(R.id.TextView_comment_comment);

        news.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                if(e == null)
                {
                    refreshComment();
                }
            }
        });

        sl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshComment();
            }
        });

        /*
        AVObject avObject = AVObject.createWithoutData("comment", objectid);
        avObject.fetchInBackground("owner", new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                mName.setText(avObject.getAVUser("owner") == null ? "" : avObject.getAVUser("owner").getUsername());
                mComment.setText(avObject.getString("comment_content"));
            }
        });



        AVObject getcomment = AVObject.createWithoutData("Getcomment",objectid);

        getcomment.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                mName.setText(avObject.getString("user_email"));
                mComment.setText( avObject.getString("comment_content"));
            }
        });
        */

        TextView comment_return = (TextView) findViewById(R.id.TextView_comment_return);
        comment_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        findViewById(R.id.imageview_newscomment_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(AVUser.getCurrentUser() == null)
                {
                    Toast.makeText(NewsCommentActivity.this, "登陆用户才能评论", Toast.LENGTH_SHORT).show();
                    return;
                }

                if ("".equals(mCommentEdit.getText().toString())) {
                    Toast.makeText(NewsCommentActivity.this, "输入评论", Toast.LENGTH_SHORT).show();
                    return;
                }

                //mProgerss.setVisibility(View.VISIBLE);
                AVObject comment = new AVObject("comment");

                comment.put("comment_content", mCommentEdit.getText().toString());
                comment.put("user_email", AVUser.getCurrentUser());
                comment.put("comment_news", news);
                comment.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        refreshComment();
                        if (e == null) {
                            //mProgerss.setVisibility(View.GONE);
                            //NewsCommentActivity.this.finish();
                            news.put("news_comment_count", news.getInt("news_comment_count")+1);
                            news.saveInBackground();
                            mCommentEdit.setText("");
                        } else {
                            //mProgerss.setVisibility(View.GONE);
                            Toast.makeText(NewsCommentActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    public void refreshComment()
    {
        AVQuery<AVObject> query_comment = new AVQuery<AVObject>("comment");
        query_comment.include("user_email");
        query_comment.whereEqualTo("comment_news", news);
        query_comment.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                sl.setRefreshing(false);
                if(list != null && list.size() != 0)
                {
                    adapter.comments.clear();
                    adapter.comments.addAll(list);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    /*
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //AVAnalytics.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //AVAnalytics.onResume(this);
    }
    */
}
